import Layout from "@/layout";
import { createRouter, createWebHashHistory } from "vue-router";

const adminModules = import.meta.glob("../views/**/*.vue", {
  import: "default",
  eager: true,
});

// 静态路由
const constantRoutes = [
  {
    path: "/login",
    component: () => import("@/views/login/index.vue"),
    meta: { hidden: true },
  },
  {
    path: "/bigscreen",
    component: () => import("@/views/bigscreen/index.vue"),
    meta: { hidden: true },
  },
  {
    path: "/401",
    component: () => import("@/views/error-page/401.vue"),
    meta: { hidden: true },
  },
  {
    path: "/404",
    component: () => import("@/views/error-page/404.vue"),
    meta: { hidden: true },
  },
];

// 后台管理的配置
const adminRoute = GM_ADMIN_ROUTE_CFG.map((r) => {
  r.children = r.children.map((subR) => {
    subR.component = adminModules[`${subR.component}.vue`];
    return subR;
  });
  r.component = Layout;
  return r;
});

// 定义路由
const routes = [...constantRoutes, ...adminRoute];

// 创建路由实例并传递 `routes` 配置
const router = createRouter({
  history: createWebHashHistory(),
  routes,
});

/**
 * 重置路由
 */
export function resetRouter() {
  router.replace({ path: "/login" });
}

export default router;
